package com.mayikt.bean;

import org.springframework.beans.BeanUtils;

/**
 * @Classname MeiteBeanUtils
 * @Description SpringBoot SpringCloud 构建微服务电商平台。
 * @Created by CHEN JIN LONG
 * @Date 2019/1/24 22:47
 * @Version 1.0
 */
public class MeiteBeanUtils<Dto, Do> {

    /**
     * DTO转DO
     *
     * @param objDto
     * @param doClass
     * @param <Do>
     * @return
     */
    public static <Do> Do dtoToDO(Object objDto, Class<Do> doClass) {
        if (objDto == null)
            return null;
        if (doClass == null)
            return null;
        try {
            Do newInstance = doClass.newInstance();
            BeanUtils.copyProperties(objDto, newInstance);
            return newInstance;
        } catch (Exception e) {
            return null;
        }
    }


    /**
     * DO转DTO
     *
     * @param objDo
     * @param dtoClass
     * @param <Dto>
     * @return
     */
    public static <Dto> Dto doToDto(Object objDo, Class<Dto> dtoClass) {
        if (objDo == null)
            return null;
        if (dtoClass == null)
            return null;
        try {
            Dto newInstance = dtoClass.newInstance();
            BeanUtils.copyProperties(objDo, newInstance);
            return newInstance;
        } catch (Exception e) {
            return null;
        }
    }
}
